
#################################################################################################

#  Description: Replicating further analyses (Appendix C and D) from Fleming, Thomas G. (Forthcoming) 
#               'Why Change a Winning Team? Explaining Post-Election Cabinet Reshuffles in Four
#                Westminster Democracies', Political Studies.
#  Author:      Thomas G. Fleming
#  Date:        10/09/2021
#  Note:        Requires file "reshuffles_data.csv" to be saved in same folder

#################################################################################################

rm(list=ls())
library(stargazer)  # for stargazer()
library(MASS)       # for glm.nb()
library(DescTools)  # for PseudoR2

library(rstudioapi) # sets location as wd
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path))

####################################
# Loads data and formats variables #
####################################

  data <- read.csv("./reshuffles_data.csv")
   
  data$election.date <- as.Date(data$election.date, format="%Y-%m-%d")  # formats as date
  data$cabinet.date <- as.Date(data$cabinet.date, format="%Y-%m-%d")    # formats as date
  
  data$selection.rule <- relevel(data$selection.rule, ref = "pm")       # sets reference category
      
################################################################################################
# Creates dependent variable - number of pre-election ministers moved within or out of cabinet #
################################################################################################
  
  data$cabinet.changes <- data$moved + data$left
  
########################################################################################
# Tables C1 and C2: Model probability of PM moving their finance or foreign ministers  #
########################################################################################

  # finance minister and seat share
  
  finance.seatshare <- glm(finance.moved ~ 
                             seatshare +
                             finance.tenure +
                             new.PM +
                             as.factor(country),
                           data = data[data$finance.available == 1,],  # excludes cases where minister had left parliament
                           family = "binomial")
  finance.seatshare.R2 <- PseudoR2(finance.seatshare, which = "McFadden")
  
  # finance minister and seat share change
  
  finance.seatshare.change <- glm(finance.moved ~ 
                                    seatshare.change +
                                    finance.tenure +
                                    new.PM +
                                    as.factor(country),
                                  data = data[data$finance.available == 1,], # excludes cases where minister had left parliament
                                  family = "binomial")
  finance.seatshare.change.R2 <- PseudoR2(finance.seatshare.change, which = "McFadden")
  
  # combining finance minister results in single regression table
  
  vars = c("seatshare", "seatshare.change", "finance.tenure", "new.PM",
           "Constant")
  
  stargazer(finance.seatshare,
            finance.seatshare.change,
            type = "html",
            title = "Table C1: Logistic regression of probability of finance minister being moved after election",
            out = "./tableC1.html",
            keep = paste0("^", vars, "$"),
            order = paste0("^", vars, "$"),
            covariate.labels = c("Seat share", "Seat share change", "Finance minister's tenure",
                                 "PM's first election", "Constant"),
            omit.stat = c("ll", "theta", "aic"),
            no.space = TRUE,
            dep.var.caption = "DV: Finance minister moving post",
            dep.var.labels.include = FALSE,
            model.numbers = FALSE,
            column.labels = c("(C1)", "(C2)"),
            add.lines = list(c("Country FE", "Yes", "Yes"),
                             c("Pseudo R2", round(finance.seatshare.R2, digits = 3),
                               round(finance.seatshare.change.R2, digits = 3))),
            font.size = "small")
  
  # foreign minister and seat share
  
  foreign.seatshare <- glm(foreign.moved ~ 
                             seatshare +
                             foreign.tenure +
                             new.PM +                             
                             as.factor(country),
                           data = data[data$foreign.available == 1,],  # excludes cases where minister had left parliament
                           family = "binomial")
  foreign.seatshare.R2 <- PseudoR2(foreign.seatshare, which = "McFadden")
  
  # foreign minister and seat share change
  
  foreign.seatshare.change <- glm(foreign.moved ~ 
                                    seatshare.change +
                                    foreign.tenure +
                                    new.PM +
                                    as.factor(country),
                                  data = data[data$foreign.available == 1,],  # excludes cases where minister had left parliament
                                  family = "binomial")
  foreign.seatshare.change.R2 <- PseudoR2(foreign.seatshare.change, which = "McFadden")
  
  # combining foreign minister results in single regression table
  
  vars = c("seatshare", "seatshare.change", "foreign.tenure", "new.PM",
           "Constant")
  
  stargazer(foreign.seatshare,
            foreign.seatshare.change,
            type = "html",
            title = "Table C2: Logistic regression of probability of foreign minister being moved after election",
            out = "./tableC2.html",
            keep = paste0("^", vars, "$"),
            order = paste0("^", vars, "$"),
            covariate.labels = c("Seat share", "Seat share change", "Foreign minister's tenure",
                                 "PM's first election", "Constant"),
            omit.stat = c("ll", "theta", "aic"),
            no.space = TRUE,
            dep.var.caption = "DV: Foreign minister moving post",
            dep.var.labels.include = FALSE,
            model.numbers = FALSE,
            column.labels = c("(C3)", "(C4)"),
            add.lines = list(c("Country FE", "Yes", "Yes"),
                             c("Pseudo R2", round(foreign.seatshare.R2, digits = 3),
                               round(foreign.seatshare.change.R2, digits = 3))),
            font.size = "small")
  
###############################################################################################################
# Table D1: Repeats main seat share models but without PMs who went on to retire voluntarily during next term #
###############################################################################################################

  # producing data set which excludes any who voluntarily retired (or are still in office)
  
  reduced.data <- data[data$pm.departure != "voluntary retirement" & data$pm.departure != "NA - still in office",]

  # bivariate (controlling just for total ministers)

  seatshare.bivariate.reduced <- glm.nb(cabinet.changes ~
                                          seatshare +
                                          preelection.total,
                                        data = reduced.data)
  seatshare.bivariate.reduced.R2 <- PseudoR2(seatshare.bivariate.reduced, which = "McFadden")

  # adding various control variables

  seatshare.controls.reduced <- glm.nb(cabinet.changes ~
                                         seatshare +
                                         preelection.total +
                                         unavailable +
                                         median.post.tenure +
                                         median.cabinet.tenure +
                                         new.PM +
                                         selection.rule,
                                       data = reduced.data)
  seatshare.controls.reduced.R2 <- PseudoR2(seatshare.controls.reduced, which = "McFadden")

  # adding country fixed effects

  seatshare.countryFE.reduced <- glm.nb(cabinet.changes ~
                                          seatshare +
                                          preelection.total +
                                          unavailable +
                                          median.post.tenure +
                                          median.cabinet.tenure +
                                          new.PM +
                                          selection.rule +
                                          as.factor(country),
                                        data = reduced.data)
  seatshare.countryFE.reduced.R2 <- PseudoR2(seatshare.countryFE.reduced, which = "McFadden")

  # replacing country fixed effects with party fixed effects

  seatshare.partyFE.reduced <- glm.nb(cabinet.changes ~
                                        seatshare +
                                        preelection.total +
                                        unavailable +
                                        median.post.tenure +
                                        median.cabinet.tenure +
                                        new.PM +
                                        selection.rule +
                                        as.factor(party),
                                      data = reduced.data)
  seatshare.partyFE.reduced.R2 <- PseudoR2(seatshare.partyFE.reduced, which = "McFadden")
  
  # creating combined table of coefficients

  vars = c("seatshare", "preelection.total", "unavailable", "median.post.tenure", "median.cabinet.tenure", "new.PM", 
           "selection.rulecaucus", "Constant")
  
  stargazer(seatshare.bivariate.reduced, 
            seatshare.controls.reduced, 
            seatshare.countryFE.reduced, 
            seatshare.partyFE.reduced,
            type = "html",
            title = "Table D1: Negative binomial regression of post-election cabinet changes",
            out = "./tableD1.html",
            keep = paste0("^", vars, "$"),
            order = paste0("^", vars, "$"),
            covariate.labels = c("Seat share", "Total ministers", "Unavailable ministers", "Median tenure (current post)", "Median tenure (any post)",
                                 "PM's first election", "Caucus selection", "Constant"),
            omit.stat = c("n", "ll", "theta", "aic"),
            no.space = TRUE,
            dep.var.caption = "DV: Ministers moved from pre-election post",
            dep.var.labels.include = FALSE,
            model.numbers = FALSE,
            column.labels = c("(D1)", "(D2)", "(D3)", "(D4)"),
            add.lines = list(c("Country FE", "No", "No", "Yes", "No"),
                   c("Party FE", "No", "No", "No", "Yes"),
                   c("Observations", nobs(seatshare.bivariate.reduced), nobs(seatshare.controls.reduced), 
                     nobs(seatshare.countryFE.reduced), nobs(seatshare.partyFE.reduced)),
                   c("Pseudo R2", round(seatshare.bivariate.reduced.R2, digits = 3),
                     round(seatshare.controls.reduced.R2, digits = 3),
                     round(seatshare.countryFE.reduced.R2, digits = 3),
                     round(seatshare.partyFE.reduced.R2, digits = 3))),
            font.size = "small")
  
######################################################################################################################
# Table D2: Repeats main seat share change models but without PMs who went on to retire voluntarily during next term #
######################################################################################################################
  
  # bivariate (controlling just for total ministers)
  
  seatshare.change.bivariate.reduced <- glm.nb(cabinet.changes ~
                                                 seatshare.change +
                                                 preelection.total,
                                               data = reduced.data)
  seatshare.change.bivariate.reduced.R2 <- PseudoR2(seatshare.change.bivariate.reduced, which = "McFadden")
  
  # adding various control variables
  
  seatshare.change.controls.reduced <- glm.nb(cabinet.changes ~
                                                seatshare.change +
                                                preelection.total +
                                                unavailable +
                                                median.post.tenure +
                                                median.cabinet.tenure +
                                                new.PM +
                                                selection.rule,
                                              data = reduced.data)
  seatshare.change.controls.reduced.R2 <- PseudoR2(seatshare.change.controls.reduced, which = "McFadden")
  
  # adding country fixed effects
  
  seatshare.change.countryFE.reduced <- glm.nb(cabinet.changes ~
                                                 seatshare.change +
                                                 preelection.total +
                                                 unavailable +
                                                 median.post.tenure +
                                                 median.cabinet.tenure +
                                                 new.PM +
                                                 selection.rule +
                                                 as.factor(country),
                                               data = reduced.data)
  seatshare.change.countryFE.reduced.R2 <- PseudoR2(seatshare.change.countryFE.reduced, which = "McFadden")
  
  # replacing country fixed effects with party fixed effects
  
  seatshare.change.partyFE.reduced <- glm.nb(cabinet.changes ~
                                               seatshare.change +
                                               preelection.total +
                                               unavailable +
                                               median.post.tenure +
                                               median.cabinet.tenure +
                                               new.PM +
                                               selection.rule +
                                               as.factor(party),
                                             data = reduced.data)
  seatshare.change.partyFE.reduced.R2 <- PseudoR2(seatshare.change.partyFE.reduced, which = "McFadden")
  
  # combining results in single regression table
  
  vars = c("seatshare.change", "preelection.total", "unavailable", "median.post.tenure", "median.cabinet.tenure", "new.PM", 
           "selection.rulecaucus", "Constant")
  
  stargazer(seatshare.change.bivariate.reduced, 
            seatshare.change.controls.reduced, 
            seatshare.change.countryFE.reduced, 
            seatshare.change.partyFE.reduced,
            type = "html",
            title = "Table D2: Negative binomial regression of post-election cabinet changes",
            out = "./tableD2.html",
            keep = paste0("^", vars, "$"),
            order = paste0("^", vars, "$"),
            covariate.labels = c("Seat share change", "Total ministers", "Unavailable ministers", "Median tenure (current post)", "Median tenure (any post)",
                                 "PM's first election", "Caucus selection", "Constant"),
            omit.stat = c("n", "ll", "theta", "aic"),
            no.space = TRUE,
            dep.var.caption = "DV: Ministers moved from pre-election post",
            dep.var.labels.include = FALSE,
            model.numbers = FALSE,
            column.labels = c("(D5)", "(D6)", "(D7)", "(D8)"),
            add.lines = list(c("Country FE", "No", "No", "Yes", "No"),
                             c("Party FE", "No", "No", "No", "Yes"),
                             c("Observations", nobs(seatshare.change.bivariate.reduced), nobs(seatshare.change.controls.reduced), 
                               nobs(seatshare.change.countryFE.reduced), nobs(seatshare.change.partyFE.reduced)),
                             c("Pseudo R2", round(seatshare.change.bivariate.reduced.R2, digits = 3),
                               round(seatshare.change.controls.reduced.R2, digits = 3),
                               round(seatshare.change.countryFE.reduced.R2, digits = 3),
                               round(seatshare.change.partyFE.reduced.R2, digits = 3))),
            font.size = "small")
  
########################################################################################## 

  rm(list=ls())
  